package cn.alone.product.service.impl;
import cn.alone.basic.service.impl.BaseServiceImpl;
import cn.alone.org.domain.Employee;
import cn.alone.org.service.IEmployeeService;
import cn.alone.product.domain.Product;
import cn.alone.product.domain.ProductDetail;
import cn.alone.product.mapper.ProductMapper;
import cn.alone.product.service.IProductDetailService;
import cn.alone.product.service.IProductService;
import cn.alone.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务
 * @author lanan
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IProductDetailService productDetailService;

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaletime", new Date());
        productMapper.onSale(map);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaletime", new Date());
        productMapper.offSale(map);
    }

    /**
     * 添加服务
     * @param product
     * @param currentLogininfo
     */
    @Override
    public void handle(Product product, Logininfo currentLogininfo) {
        Employee employee = employeeService.loadByLogininfoId(currentLogininfo.getId());
        if(employee.getShop_id()!=null){
            product.setShop_id(employee.getShop_id());
        }
        /*返回自增id*/
        product.setCreatetime(new Date());
        productMapper.save(product);
        if (product.getDetail()!=null){
            ProductDetail productDetail = product.getDetail();
            productDetail.setProduct_id(product.getId());
            productDetailService.add(productDetail);
        }
    }

    @Override
    public Product loadByIdDetail(Long id) {
        return productMapper.loadByIdDetail(id);
    }
}
